<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use api\wxapp\model\GoodsActivity;
use api\wxapp\model\ShopGoods;
use api\wxapp\model\Goods;
class GoodsActivityController extends AdminBaseController
{
    private $shop_id;
    private $where = array();
    private $params = array();
    public function initialize()
    {
        parent::initialize();
        $sessionAdminId = session('ADMIN_ID');
        if($sessionAdminId !== 1){
           //说明是店铺登陆
           $find = db::name('app_shop')->where('admin_id',$sessionAdminId)->find();
           $this->shop_id = $find['id'];
           $this->where[] = ['t1.shop_id','eq',$find['id']];
        }
        $this->shop_id = 1;
    }
    //店铺商品活动
    public function index(GoodsActivity $GoodsActivity)
    {
        $type = 0;
        $query = GoodsActivity::alias('t1');
        //$query = $GoodsActivity->leftgoods($query);
        $query = $GoodsActivity->leftshop($query);
        $query = $GoodsActivity->returnfiled($query);
        $params = $this->param();
        if(isset($params['name'])){
            $this->where[] = ['t3.name','like','%'.$params['name'].'%'];
        }
        if(isset($params['type'])){
            if($params['type'] == 1){//已结束
                $this->where[] = ['t1.etime','lt',date('Y-m-d H:i',time())];
            }
            if($params['type'] == 2){//正在进行
                $this->where[] = ['t1.stime','elt',date('Y-m-d H:i',time())];
                $this->where[] = ['t1.etime','egt',date('Y-m-d H:i',time())];
            }
            if($params['type'] == 3){//暂未开始
                $this->where[] = ['t1.stime','gt',date('Y-m-d H:i',time())];
            }
            $type = $params['type'];
        }
        $list =  $query->where($this->where)->paginate(10,false,
        ['query'=>$params])->each(function($item, $key){
            $value = json_decode($item['value']);
            $str = '';
            foreach($value as $k=>$v){
                $str .= Goods::where('id',$k)->value('name').':'.$v.',';
            }
            if($item['etime'] < date('Y-m-d H:i',time())){
                $item['type'] = '已结束';
            }elseif($item['etime'] > date('Y-m-d H:i',time()) && $item['stime'] < date('Y-m-d H:i',time())){
                $item['type'] = '正在进行';
            }else{
                $item['type'] = '暂未开始';
            }
            $item['goods'] = rtrim($str,',');
            return $item;
        });
        $this->assign('type',$type);
        $this->assign('list',$list);
        $this->assign('page', $list->render());
        return $this->fetch();
    }
    //编辑商品活动
    public function edit(GoodsActivity $GoodsActivity)
    {   
        if($_POST){
            // if(session('ADMIN_ID') == 1){
            //     $this->error('此权限只给店铺开放!!!');
            // }
            $GoodsActivity->check($_POST) ? $this->error($GoodsActivity->check($_POST)) : '';
            if($_POST['stime'] > $_POST['etime']){
                $this->error('开始时间大于结束时间!!!');
            }
            $value = array();
            foreach($_POST['goods_id'] as $k=>$v){
                if(!$_POST['goods_price'][$k]){
                    $this->error('请设置秒杀商品价格!!!');
                }
                $value[$v] = $_POST['goods_price'][$k];
            }
            //查询此商品是否已有活动并且暂未过期
            $map[] = ['shop_id','eq',$this->shop_id];
            //不能同时开展两个秒杀活动 前一个活动的结束时间 必须小于 新添加活动的开始时间
            $map[] = ['etime','elt',$_POST['stime']];
            $find = $GoodsActivity->where($map)->find();
            if($find){
                $this->error('有秒杀活动未结束,不能不是发布两个秒杀活动!!!!!');
            }
            $data['id'] = $_POST['id'];
            $data['stime'] = $_POST['stime'];
            $data['etime'] = $_POST['etime'];
            $data['value'] = json_encode($value);
            $res = GoodsActivity::update($data);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        $params = $this->param();
        // $query = GoodsActivity::alias('t1');
        // $query = $GoodsActivity->leftgoods($query);
        // $query = $GoodsActivity->leftshop($query);
        // $query = $GoodsActivity->returnfiled($query);
        $info =  GoodsActivity::get($params['id']);
        $info ['value'] = json_decode($info['value'],true);
        //查询 该店铺 在售所有商品
        $goods = $GoodsActivity->getgoods($this->shop_id);
        $this->assign('goods',$goods);
        $this->assign('info',$info);
        return $this->fetch();
    }

    //添加活动
    public function add(GoodsActivity $GoodsActivity)
    {
        if($_POST){
            if(session('ADMIN_ID') == 1){
                $this->error('此权限只给店铺开放!!!');
            }
            $GoodsActivity->check($_POST) ? $this->error($GoodsActivity->check($_POST)) : '';
            if($_POST['stime'] > $_POST['etime']){
                $this->error('开始时间大于结束时间!!!');
            }
            $value = array();
            foreach($_POST['goods_id'] as $k=>$v){
                if(!$_POST['goods_price'][$k]){
                    $this->error('请设置秒杀商品价格!!!');
                }
                $value[$v] = $_POST['goods_price'][$k];
            }
            //查询此商品是否已有活动并且暂未过期
            $map[] = ['shop_id','eq',$this->shop_id];
            //不能同时开展两个秒杀活动 前一个活动的结束时间 必须小于 新添加活动的开始时间
            $map[] = ['etime','elt',$_POST['stime']];
            $find = $GoodsActivity->where($map)->find();
            if($find){
                $this->error('有秒杀活动未结束,不能不是发布两个秒杀活动!!!!!');
            }
            $data['shop_id'] = $this->shop_id;
            $data['addtime'] = date('Y-m-d H:i:s');//json_encode
            $data['stime'] = $_POST['stime'];
            $data['etime'] = $_POST['etime'];
            $data['value'] = json_encode($value);
            $res = GoodsActivity::insert($data);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        $goods = $GoodsActivity->getgoods($this->shop_id);
        $this->assign('goods',$goods);
        return $this->fetch();
    }
}
